package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sky.entity.Dish;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.SetmealService;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;

/**
 * @author wjh
 * @create 2023-07-12
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    
    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private UserMapper userMapper;

    @Resource
    private DishMapper dishMapper;

    @Resource
    private SetmealMapper setmealMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
        HashMap<String, Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        Integer orderCount = ordersMapper.getOrderCount(map);
        map.put("status", Orders.COMPLETED);
        Double Turnover = ordersMapper.sumByMap(map);
        Turnover = Turnover == null ? 0.0 : Turnover;
        Integer validOrderCount = ordersMapper.getOrderCount(map);
        validOrderCount = validOrderCount == null ? 0 :validOrderCount;
        Integer newUsers = userMapper.countNewUsersByMap(map);
        newUsers = newUsers == null ? 0 : newUsers;
        businessDataVO.setTurnover(Turnover);//营业额
        businessDataVO.setValidOrderCount(validOrderCount);//有效订单数
        businessDataVO.setNewUsers(newUsers);//新增用户数
        if (validOrderCount == 0) {
            businessDataVO.setUnitPrice(0.0);//平均客单价
        } else {
            double v = new BigDecimal(Turnover / validOrderCount).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
            businessDataVO.setUnitPrice(v);//平均客单价
        }
        if (orderCount == 0) {
            businessDataVO.setOrderCompletionRate(0.0);//订单完成率
        }else {
            businessDataVO.setOrderCompletionRate(validOrderCount.doubleValue()/orderCount);//订单完成率
        }


        return businessDataVO;
    }

    @Override
    public OrderOverViewVO getOrderOverView() {
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        HashMap<String, Object> map = new HashMap<>();
        LocalDate now = LocalDate.now();
        LocalDateTime begin = LocalDateTime.of(now, LocalTime.MIN);
        LocalDateTime end = LocalDateTime.of(now, LocalTime.MAX);
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",Orders.TO_BE_CONFIRMED);
        orderOverViewVO.setWaitingOrders(ordersMapper.getOrderCount(map));
        map.put("status",Orders.CONFIRMED);
        orderOverViewVO.setDeliveredOrders(ordersMapper.getOrderCount(map));
        map.put("status",Orders.COMPLETED);
        orderOverViewVO.setCompletedOrders(ordersMapper.getOrderCount(map));
        map.put("status",Orders.CANCELLED);
        orderOverViewVO.setCancelledOrders(ordersMapper.getOrderCount(map));
        map.put("status",null);
        orderOverViewVO.setAllOrders(ordersMapper.getOrderCount(map));
        return orderOverViewVO;
    }

    @Override
    public DishOverViewVO getDishOverView() {
        DishOverViewVO dishOverViewVO = new DishOverViewVO();
        dishOverViewVO.setSold(dishMapper.selectCountByStatus(1));
        dishOverViewVO.setDiscontinued(dishMapper.selectCountByStatus(0));
        return dishOverViewVO;
    }

    @Override
    public SetmealOverViewVO getSetmealOverView() {
        SetmealOverViewVO setmealOverViewVO = new SetmealOverViewVO();
        setmealOverViewVO.setSold(setmealMapper.selectCountByStatus(1));
        setmealOverViewVO.setDiscontinued(setmealMapper.selectCountByStatus(0));
        return setmealOverViewVO;
    }
}
